package model;

import java.security.InvalidParameterException;

public class Rank
{
    private static final int Min = 1;
    private static final int Max = 8;
    private static final int Count = 8;
    
    public static int GetValue(int rank) throws InvalidParameterException
    {
        if(isValid(rank))
        {
            return rank - 1;
                
        }
        
        throw new InvalidParameterException(String.format("rank value: '%d' is invalid", rank));
    }
    
    public static int[] GetRanks()
    {
        int[] ranks = new int[Count];
        int rank = Min;
        
        for(int i = 0; i < Count; i++)
        {
            ranks[i] = rank++;
        }
        
        return ranks;
    }
     
    public static int next(int rank) throws InvalidParameterException
    {        
        if(hasNext(rank))
        {
            return rank + 1;            
        }
        
        throw new InvalidParameterException(String.format("rank value: '%d' doesn't have a next value", rank));
    }
    
    public static int previous(int rank) throws InvalidParameterException
    {        
        if(hasPrevious(rank))
        {
            return rank - 1;    
        }
        
        throw new InvalidParameterException(String.format("rank value: '%d' doesn't have a previous value", rank));
    }
    
    public static boolean hasPrevious(int rank)
    {
        return GetValue(rank) > 0;
    }
    
    public static boolean hasNext(int rank)
    {
        return GetValue(rank) < Count - 1;
    }
    
    public static int ahead(int rank, Color color)
    {
        return color == Color.White? next(rank) : previous(rank);
    }
    
    private static boolean isValid(int rank)
    {
        return Min <= rank && rank <= Max;
    }
}

